package com.choupin.choupin.controller;


import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.entity.Order;
import com.choupin.choupin.service.IOderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    IOderService oderService;

//    生成订单
     @CrossOrigin
     @PostMapping("/create")
     public JsonResult<Void> createOrder(@RequestBody Map<String,Object> data ){
         JsonResult<Void> jsonResult=new JsonResult<>();
         System.out.println("来到订单控制器！"+(String) data.get("phone")+","+(String)data.get("name"));
         oderService.createOrder((String) data.get("phone"),(String)data.get("name"),(Integer)data.get("type"), (Integer) data.get("memeberId"));
         jsonResult.setState(SUCCESS);
         return  jsonResult;
     }

//     查询订单
     @CrossOrigin
     @PostMapping("/showorder")
     public JsonResult<List<Order>> showOrder(@RequestBody Map<String,Object> data){
         JsonResult<List<Order>> jsonResult=new JsonResult<>();
         System.out.println((String) data.get("phone")+","+(Integer) data.get("currentPage")+(Integer) data.get("pageSize"));
         List<Order> orders=oderService.showOrder((String) data.get("phone"),(Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
         jsonResult.setState(SUCCESS);
         jsonResult.setData(orders);
         return  jsonResult;
     }

    //    查看倒计时
    @CrossOrigin
    @RequestMapping("/countdown")
    public  JsonResult<Integer> countDownTime(@RequestBody Map<String,Object> data){
        JsonResult<Integer> jsonResult=new JsonResult<>();
        System.out.println("订单id为："+(Integer) data.get("id"));
        Integer result=oderService.countDown((Integer) data.get("id"));
        if (result==-1){
            jsonResult.setState(-1);
        }else {
            jsonResult.setState(SUCCESS);
            jsonResult.setData(result);
        }
        return jsonResult;
    }


    @CrossOrigin
    @RequestMapping("/updateorder")
     public JsonResult<Void> updateOrder(@RequestBody Map<String,Object> data ){
         JsonResult<Void> jsonResult=new JsonResult<>();
         oderService.updateOrder((Integer) data.get("id"),(String) data.get("phone"));
         jsonResult.setState(SUCCESS);
         return  jsonResult;
    }
}
